﻿#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <sys/timeb.h>

#include "common_func.h"

using namespace std;



unsigned short Reversal16(unsigned short data)
{
	unsigned short reversal = (data & 0xff) << 8 | (data >> 8);
	return reversal;
}

//数组字节反转
void ReversalArray(unsigned char * data, int len)
{	
	char *pBak = new char[len];

	memcpy(pBak, data, len);

	int index = len -1;
	for ( int i = 0; i < len; i++ ,index--)
	{
		data[index] = pBak[i];
	}

	delete [] pBak;
	
}



//累加校验和
unsigned short sumCheck(unsigned char *data, int len)
{
	unsigned short sum = 0;
	for(int i  = 0 ; i < len; i++)
	{
		sum += data[i];
	}

	return sum;
}

unsigned char CheckSum(unsigned char *buf, int len)
{ 
    int i;
	unsigned char ret = 0;

    for( i = 0; i < len; i++ )
    {
        ret += buf[i];
    }
    return ret;
}

//科士达UPS使用
unsigned char kstar_length_sumCheck(const unsigned char  *data)
{
	unsigned char sum = 0;

    sum = data[0] & 0x0f ;
    sum += data[1] & 0x0f;
    sum += (data[1] >> 4) & 0x0f;
	
	sum = sum % 0x10;
	sum = ~sum; 
	sum += 1;

	sum = ((sum & 0x0f) << 4) & 0xf0;//返回低4bit位
	return sum; 
}

//电总协议使用
unsigned short kstar_msg_sumCheck(const unsigned char  *data, int len)
{
	unsigned short sum = 0;
	for(int i  = 0 ; i < len; i++)
	{
		sum += data[i];
	}

	sum = sum%0xffff;
	sum = ~sum; 
	sum += 1;
	
	return sum;
}











